/**
 * @Created by zhuly6 zhuly8@yusys.com.cn on 2019-7-31 09:59:16.
 * @updated by
 * @description powerCenter参数管理
 */
define(['./custom/widgets/js/yufpSelectParam.js'],
	function (require, exports) {
		/**
		 * 页面加载完成时触发
		 * @param hashCode 路由ID
		 * @param data 传递数据对象
		 * @param cite 页面站点信息
		 */
		exports.ready = function (hashCode, data, cite) {
			yufp.lookup.reg('PROXY_FLAG');
			yufp.custom.vue({
				el: cite.el,
				data: function () {
					var _this = this;
					/**
					 * 在 状态参数名 为空时, 返回参数名 字段为必填项
					 */
					var returnParamValidate = function (rule, value, callback) {
						if (value === '' && _this.formdata.statusParamName === '') {
							callback(new Error('状态参数名为空时这里必填'));
						} else {
							callback();
						}
					};
					/**
					 * 校验自定义参数代码字段，必须以 $ 开头
					 */
					var paramCodeValidate = function (rule, value, callback) {
						var reg = /^\$/;
						if (value && reg.test(value)) {
							callback();
						} else if (value && !reg.test(value)) {
							callback(new Error('校验自定义参数代码字段，请以 $ 开头'));
						} else if (value === '') {
							callback(new Error('校验自定义参数代码字段，请以 $ 开头'));
						} else {
							callback();
						}
					};
					/**
					 * 校验输入的字符串是否符合 IP地址:端口号 的格式，多个这样的字符串用英文逗号分隔
					 */
					var ipAdressPortValidate = function (rule, value, callback) {
						var reg = /^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|[a-z|A-Z|0-9|_|-]+):([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$/;
						var addressArr = value.split(',');
						for (var i = 0; i < addressArr.length; ++i) {
							if (addressArr[i] && reg.test(addressArr[i])) {
								continue;
							} else if (addressArr[i] && !reg.test(addressArr[i])) {
								callback(new Error('服务器地址格式为ip:port，多个ip用,分隔'));
							} else {
								continue;
							}
						}
						callback();
					};
					return {
						searchFormdata: {},
						addFnButton: !yufp.session.checkCtrl('addFn'),
						updateFnButton: !yufp.session.checkCtrl('updateFn'),
						deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
						treeDataUrl: "api/use/plats/apps/getTreeWftList",
						dataUrl: 'api/use/plats/pccfg/queryList',
						sysDataUrl: '',
						saveBtnShow: true,
						cancelBtnShow: true,
						saveDisabled: false,
						formdata: {},
						async: false,
						param: { UNITID: '0000', LEVELUNIT: '1' },
						height: yufp.frame.size().height,
						dialogVisible: false,
						viewTitleArrry: ['PowerCenter参数新增', 'PowerCenter参数修改', 'PowerCenter参数查看'],
						viewTitle: '',
						propTabaleData: [],
						addFlag: true,
						refTemp: [],
						nodeLeaf: false,
						propTableData: [],
						agentIds: [
						],
						rule: [
							{ validator: returnParamValidate, trigger: 'blur' },
							{ validator: paramCodeValidate, trigger: 'blur' },
							[{ required: true, message: '必填项', trigger: 'blur' },
							{ validator: ipAdressPortValidate }]
						],
						nodeTemp: {},
						isReadOnly: false
					};
				},
				methods: {

					reloadData: function () {
						var _this = this;
						var paramObj = _this.$refs.queryModelRef.fm;
						if (_this.nodeTemp.appCode != null && _this.nodeTemp.appCode != '0000') {
							paramObj.appCode = _this.nodeTemp.appCode;
						}
						_this.$refs.refTable.remoteData(paramObj);
					},

					/**
					 * 取消
					 */
					cancelFn: function () {
						var _this = this;
						_this.dialogVisible = false;
					},
					/**
					 * 保存
					 */
					saveFn: function () {

						var _this = this;
						var validate = false;
						_this.$refs.refForm.validate(function (valid) {
							validate = valid;
						});
						if (!validate) {
							return;
						}

						_this.checkClassCode(validate);

						if (!validate) {
							return;
						}

						var paramObj = _this.formdata;


						if (paramObj.useParamValues != null) {
							paramObj.useParamValues.splice(0);
						} else {
							paramObj.useParamValues = new Array();
						}

						for (let i = 0; i < _this.propTableData.length; i++) {
							paramObj.useParamValues.push(_this.propTableData[i]);
						}

						let urlparam = "";
						if (_this.addFlag) {
							paramObj.appCode = _this.nodeTemp.appCode;
							urlparam = 'api/use/plats/pccfg/saveUsePcCfgPo';
						} else {
							urlparam = 'api/use/plats/pccfg/updateUsePcCfgPo';
						}


						yufp.service.request({
							method: 'POST',
							url: urlparam,
							data: paramObj,
							callback: function (code, message, response) {

								if (response.code == 0) {
									_this.reloadData();

									_this.$message('操作成功');
									_this.dialogVisible = false;
								} else {
									_this.$message({ message: response.message, type: "warning" });
								}
							}
						});


					},
					/**
					 * 控制保存按钮、xdialog、表单的状态
					 * @param viewType 表单类型
					 * @param editable 可编辑,默认false
					 */
					switchStatus: function (viewType, editable) {
						var _this = this;
						_this.viewType = viewType;
						_this.dialogVisible = true;
						_this.saveBtnShow = editable;
						_this.cancelBtnShow = editable;
					},
					nodeClickFn: function (nodeData, node, self) {
						var _this = this;

						if (nodeData.appCode != '0000') {
							_this.nodeLeaf = true;
							_this.nodeTemp.appCode = nodeData.appCode;
							_this.$refs.refTable.remoteData({ appCode: _this.nodeTemp.appCode });

						} else {
							_this.nodeLeaf = false;
							_this.nodeTemp.appCode = null;
							_this.$refs.refTable.remoteData({ appCode: "" });
						}

					},

					/**
					 * 新增
					 */
					addFn: function () {
						var _this = this;
						_this.isReadOnly = false;
						_this.dialogVisible = true;
						_this.saveDisabled = false;
						_this.viewTitle = _this.viewTitleArrry[0];
						_this.addFlag = true;
						_this.getAgentIds(_this.nodeTemp.appCode);

						_this.$nextTick(function () {
							_this.viewTitle = _this.viewTitleArrry[0];
							_this.$refs.refForm.resetFields();
							_this.propTableData.splice(0);
							_this.formdata.UserName = '';
							_this.formdata.password = '';
						});
					},
					/**
					 * 修改
					 */
					modifyFn: function () {
						var _this = this;
						if (_this.$refs.refTable.selections.length != 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
							return;
						}

						_this.isReadOnly = true;
						_this.nodeTemp.appCode = _this.$refs.refTable.selections[0].appCode;
						_this.getAgentIds(_this.$refs.refTable.selections[0].appCode);
						_this.viewTitle = _this.viewTitleArrry[1];
						_this.saveDisabled = false;
						_this.dialogVisible = true;
						_this.addFlag = false;
						_this.refTemp = _this.$refs.refTable.selections[0];
						var data = _this.$refs.refTable.selections[0].useParamValues;
						_this.$nextTick(function () {
							_this.$refs.refForm.resetFields();
							_this.propTableData.splice(0);
							yufp.clone(_this.refTemp, _this.formdata);
							// _this.$refs.refPropTable.remoteData(propTableParams);
							if (data != null)
								for (var i = 0; i < data.length; i++) {
									_this.propTableData.push(data[i]);
								}
						});
					},

					viewFn: function (obj) {

						var _this = this;
						_this.isReadOnly = true;
						_this.nodeTemp.appCode = obj.appCode;
						_this.nodeTemp.appCode = obj.appCode;
						_this.getAgentIds(obj.appCode);
						_this.viewTitle = _this.viewTitleArrry[2];
						_this.saveDisabled = true;
						_this.dialogVisible = true;
						_this.addFlag = false;
						_this.refTemp = obj;
						var data = obj.useParamValues;
						_this.$nextTick(function () {
							_this.$refs.refForm.resetFields();
							_this.propTableData.splice(0);
							yufp.clone(_this.refTemp, _this.formdata);
							// _this.$refs.refPropTable.remoteData(propTableParams);
							if (data != null)
								for (var i = 0; i < data.length; i++) {
									_this.propTableData.push(data[i]);
								}
						});

					},

					/**
					 * 删除
					 */
					deleteFn: function () {
						var _this = this;
						var selections = _this.$refs.refTable.selections;
						if (selections.length < 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
							return;
						}
						var len = selections.length, arr = [];
						for (var i = 0; i < len; i++) {
							arr.push(selections[i].id);
						}
						_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning', duration: 6000,
							center: true,
							callback: function (action) {
								if (action === 'confirm') {
									yufp.service.request({
										method: 'DELETE',
										url: "api/use/plats/pccfg/deleteUsePcCfgPo",
										data: selections[0],
										callback: function (code, message, response) {
											if (response.code != 0) {
												isOk = false;
												_this.$message(response.message);
											} else {
												_this.$message('操作成功');
												_this.reloadData();

											}

										}
									});
								}
							}
						});
					},
					/**
					 * 获取参数表数据
					 */
					getPropTableData: function (data, total) {
						var _this = this;
						_this.propTabaleData.splice(0);
						for (var i = 0; i < data.length; i++) {
							_this.propTabaleData.push(data[i]);
						}
					},
					/**
					 * 新增参数
					 */
					propAddFn: function () {
						var _this = this;
						var obj = {
							paramCode: '',
							paramName: '',
							paramValue: ' '
						};
						_this.propTableData.push(obj);
					},
					/**
					 * 删除参数
					 */
					propDeleteFn: function () {
						var _this = this;
						let selectDatas = _this.$refs.refPropTable.selections;
						for (let jdx = _this.propTableData.length - 1; jdx >= 0; jdx--) {
							for (let idx = 0; selectDatas.length > idx; idx++) {
								if (_this.propTableData[jdx] == selectDatas[idx]) {
									_this.propTableData.splice(jdx, 1);
									break;
								}
							}

						}
					},
					/**
					 * 获取参数选择的值
					 */
					getSelectedData: function () {

					},
					getAgentIds: function (appCode) {

						var _this = this;
						_this.agentIds.splice(0);
						const model = {};
						model.appCode = appCode;

						var params = { condition: JSON.stringify(model) };

						var initObj = {};
						initObj.key = "_0";
						initObj.value = "虚拟代理";
						_this.agentIds.push(initObj);
						yufp.service.request({
							method: 'GET',
							url: "api/use/plats/apps/queryWftAppList",
							data: params,
							async: false,
							callback: function (code, message, response) {

								if (response.data.length > 0) {
									const wftAgtList = response.data[0].wftAgtList;

									if (wftAgtList != null)
										for (let j = 0; j < wftAgtList.length; j++) {
											var obj = {};
											obj.key = wftAgtList[j].agentId;
											obj.value = wftAgtList[j].agentName;
											_this.agentIds.push(obj);

										}
								}
							}
						});

					},
					checkClassCode: function (isOk) {

						var _this = this;
						var paramObj = _this.formdata;
						paramObj.useParamValues = [];
						if (!_this.addFlag) {
							return;
						}

						yufp.service.request({
							method: 'GET',
							url: "api/use/plats/dscfg/checkClassCode",
							data: paramObj,
							async: false,
							callback: function (code, message, response) {
								if (response.code != 0) {
									isOk = false;
									_this.$message(response.message);
								} else {
									if (!response.data) {
										isOk = false;
										_this.$message('该驱动缩写在系统中已经存在');
									}
								}

							}
						});
					},

				}
			});
		};
	});